package com.lvyq.myplayer.ui.activity

import android.content.Intent
import android.os.Environment
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.appbar.AppBarLayout
import com.lvyq.myplayer.R
import com.lvyq.myplayer.adapter.IndexDailyPlayListAdapter
import com.lvyq.myplayer.adapter.current.CurrentPlayLstAdapter
import com.lvyq.myplayer.base.BaseActivity
import com.lvyq.myplayer.common.shared.MainAudioSharedPrefsManager
import com.lvyq.myplayer.common.util.StartActivityUtils
import com.lvyq.myplayer.model.index.IndexDailyPlayModel
import com.lvyq.myplayer.parcelable.SongInfoData
import com.lvyq.myplayer.service.AudioService
import com.squareup.picasso.Picasso
import java.lang.Math.abs

/**
 * 文件描述：每日推荐
 * 作者：lvyq
 * 创建时间：2025-03-27
 * 修改时间：2025-03-27
 */
class IndexDailyActivity: BaseActivity() {

    private lateinit var viewModel: IndexDailyPlayModel

    override var currentPlayAdapter: CurrentPlayLstAdapter? = null

    override fun getLayoutId(): Int {
        return R.layout.activity_index_daily
    }

    override fun initData() {
        initPlayBoxService()
        var recyclerView = findViewById<RecyclerView>(R.id.dailyLst)
        //设置处置列表
        recyclerView.layoutManager=LinearLayoutManager(this)

        // 确保嵌套滚动正常工作
        recyclerView.isNestedScrollingEnabled = true

        var bannerImg = findViewById<ImageView>(R.id.bannerImg)
        var content =findViewById<TextView>(R.id.content)

        //滑动图片透明度变化
        val appBarLayout = findViewById<AppBarLayout>(R.id.appBarLayout)
        appBarLayout.addOnOffsetChangedListener{ appBarLayout, verticalOffset ->
            val scrollRange = bannerImg.height // 使用 bannerImg 的高度作为滚动范围
            val percentage = abs(verticalOffset).toFloat() / scrollRange.toFloat()
            // 根据滚动位置调整透明度
            val alpha = (1 - percentage) * 255 // 从完全不透明（255）到完全透明（0）
            bannerImg.alpha = alpha / 255f // 转换为0-1范围
        }

        viewModel = ViewModelProvider(this)[IndexDailyPlayModel::class.java]
        // 观察数据变化
        viewModel.data.observe(this) { data ->
            val dailyAdapter = IndexDailyPlayListAdapter(this,data.data)
            recyclerView.adapter=dailyAdapter

            var listSong = data.data
            //点击事件
            dailyAdapter.setOnItemClickListener(object :IndexDailyPlayListAdapter.OnItemClickListener{
                override fun onItemClick(data: SongInfoData, position: Int) {
                    //播放歌曲列表存储
                    StartActivityUtils.startAudioPlayerActivityAndSaveDB(this@IndexDailyActivity,listSong,position)
                }
            })

            Picasso.with(this).load(data.data[0].bannerImg).into(bannerImg)
            content.text =data.data[0].content
        }
        // 触发数据获取
        viewModel.list()

    }

    /**
     * 播放栏服务初始化
     */
    private fun initPlayBoxService() {
        initMainPlayBoxListener()
        currentPlayAdapter=getAdapter()
        initAudioPlayLst(this)

        //设置viewpage2 适配器
        recreateViewPagerAdapter()
        initMainPlayStateChangeUI()
       // updateMainPlayStateBtn(false)
        var songInfo = MainAudioSharedPrefsManager.getSongInfo(this)
        var currentAudioPosition = MainAudioSharedPrefsManager.getCurrentAudioPosition(this)
        //通过audioservice 播放,混合启动，保证退出当前页仍然播放
        val audioIntent = Intent(this, AudioService::class.java)
        //传递值到AudioService中
        audioIntent.putExtra("data",songInfo)
        audioIntent.putExtra("from",-1)
        audioIntent.putExtra("position",currentAudioPosition?.toInt())
        //绑定 BIND_AUTO_CREATE 服务未创建时创建并绑定
        bindService(audioIntent,connection, BIND_AUTO_CREATE)
        //启动
        startService(audioIntent)
    }

    private fun getAdapter(): CurrentPlayLstAdapter {
        return CurrentPlayLstAdapter(this)
    }

    override fun onDestroy() {
        super.onDestroy()
    }


}

